package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.nhc.database.manager.QiyaCenter;
import com.nhc.model.NaviTrackSetInfo;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.BottomMenuWindow;
import zuo.biao.library.ui.EditTextInfoWindow;
import zuo.biao.library.ui.TopMenuWindow;

public class NaviTrackSettingActivity extends BaseActivity implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {
    private final String TAG = "NaviTrackSettingActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, NaviTrackSettingActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.navitracksetting_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private ScrollView svDemoMain;

    private TextView tv_ntmode;
    private TextView tv_ntintervel;
    private TextView tv_autosave;
    private ImageView iv_swith;
    private ImageView iv_swith2;
    private TextView tv_pianhangfazhi;


    @Override
    public void initView() {//必须调用
        svDemoMain = (ScrollView) findViewById(R.id.svDemoMain);
        tv_ntmode = (TextView) findViewById(R.id.tv_ntmode);
        tv_ntintervel = (TextView) findViewById(R.id.tv_ntintervel);
        tv_autosave = (TextView) findViewById(R.id.tv_autosave);
        iv_swith = (ImageView) findViewById(R.id.iv_swith);
        iv_swith2 = (ImageView) findViewById(R.id.iv_swith2);
        tv_pianhangfazhi = (TextView) findViewById(R.id.tv_pianhangfazhi);

        if (QiyaCenter.getInstance()._thisQiyaInfo == null) {
            findViewById(R.id.navitrackset_3).setVisibility(View.GONE);
        }

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    NaviTrackSetInfo _setInfo;

    @Override
    public void initData() {//必须调用
        _setInfo = NaviTrackSetInfo.getInstance();
        _setInfo.load(this);

        refresh();
    }

    void refresh() {
        tv_ntmode.setText(_setInfo.getSavemodeStr());

        tv_ntintervel.setText(_setInfo.getIntervalStr());
        tv_autosave.setText(_setInfo.getAutomodeStr());
        if (_setInfo.isUseqiyagd()) {
            iv_swith.setImageResource(R.drawable.swith_open);
        } else {
            iv_swith.setImageResource(R.drawable.swith_close);
        }

        if (_setInfo.isSaveendclear()) {
            iv_swith2.setImageResource(R.drawable.swith_open);
        } else {
            iv_swith2.setImageResource(R.drawable.swith_close);
        }

        String AA[] = SysUtils.getInstance().getAltitudeStr(_setInfo.getPhfz());
        tv_pianhangfazhi.setText(AA[0] + AA[1]);

    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须调用
        findViewById(R.id.navitrackset_0).setOnClickListener(this);
        findViewById(R.id.navitrackset_1).setOnClickListener(this);
        findViewById(R.id.navitrackset_2).setOnClickListener(this);
        findViewById(R.id.navitrackset_3).setOnClickListener(this);
        findViewById(R.id.navitrackset_4).setOnClickListener(this);
        findViewById(R.id.navitrackset_5).setOnClickListener(this);
    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            default:
                break;
            case 21://left
            case 22://right
            case 19://up
            case 20://down
            case 23://ok
                setFoucseCtrl();
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    boolean _isFirst = true;

    private void setFoucseCtrl() {
        if (!_isFirst) {
            return;
        }

        _isFirst = false;

        for (int i = 0; i < CTRL_IDS.length; i++) {
            findViewById(CTRL_IDS[i]).setFocusable(true);
        }
    }

    private int[] CTRL_IDS = {
            R.id.navitrackset_0, R.id.navitrackset_1, R.id.navitrackset_2,
            R.id.navitrackset_3, R.id.navitrackset_4, R.id.navitrackset_5};


    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void finish() {
        _setInfo.save(this);
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onForwardClick(View v) {
        //top栏右边按钮响应
        toActivity(TopMenuWindow.createIntent(context, getResources().getStringArray(R.array.top_menu)), REQUEST_TO_TOP_MENU, false);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.navitrackset_0:
                toActivity(BottomMenuWindow.createIntent(context, NaviTrackSetInfo.SAVEMODE_NAMES)
                        .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.jlms)), REQUEST_TO_BOTTOM_MENU_1, false);
                break;
            case R.id.navitrackset_1:
                if (_setInfo.getSavemode() == 0) {
                    toActivity(BottomMenuWindow.createIntent(context, NaviTrackSetInfo.INTERVALSTR_TIMES)
                            .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.jljg)), REQUEST_TO_BOTTOM_MENU_2, false);
                } else if (_setInfo.getSavemode() == 1) {
                    toActivity(BottomMenuWindow.createIntent(context, NaviTrackSetInfo.INTERVALSTR_DISTANCES)
                            .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.jljg)), REQUEST_TO_BOTTOM_MENU_2, false);
                } else {
                    showShortToast(getString(R.string.zdwxszjljg));
                }
                break;
            case R.id.navitrackset_2:
                toActivity(BottomMenuWindow.createIntent(context, NaviTrackSetInfo.AUTOMODE_NAMES)
                        .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.zdbcgj)), REQUEST_TO_BOTTOM_MENU_3, false);
                break;
            case R.id.navitrackset_3:
                onQiyaha();
                break;
            case R.id.navitrackset_4:
                editPLFZ();
                break;
            case R.id.navitrackset_5:
                onSaveendclear();
                break;
            default:
                break;
        }
    }

    private void onQiyaha() {
        boolean isQiya = _setInfo.isUseqiyagd();
        _setInfo.setUseqiyagd(!isQiya);
        refresh();
    }

    private void onSaveendclear() {
        boolean saveendclear = _setInfo.isSaveendclear();
        _setInfo.setSaveendclear(!saveendclear);
        refresh();
    }

    private void editPLFZ() {
        String strUnit = "m";
        double alt = _setInfo.getPhfz();
        switch (SysUtils.getInstance().dis_mode) {
            default:
                break;
            case 4:
            case 1:
                alt = alt * 3.2808399;
                strUnit = "ft";
                break;
            case 2:
                alt = alt * 1.0936133;
                strUnit = "yd";
                break;
        }
        intent = EditNumberActivity.createIntentD(context
                , getString(R.string.xgplfz), alt, strUnit);
        toActivity(intent, REQUEST_TO_PLFZ);
    }

    private final int REQUEST_TO_PLFZ = 23;
    private final int REQUEST_TO_TOP_MENU = 30;

    private final int REQUEST_TO_BOTTOM_MENU_1 = 31;
    private final int REQUEST_TO_BOTTOM_MENU_2 = 32;
    private final int REQUEST_TO_BOTTOM_MENU_3 = 33;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_BOTTOM_MENU_1:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    _setInfo.setSavemode(selectedPosition);
                    refresh();
                }
                break;
            case REQUEST_TO_BOTTOM_MENU_2:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    _setInfo.setIntervalmode(selectedPosition);
                    refresh();
                }
                break;
            case REQUEST_TO_BOTTOM_MENU_3:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    _setInfo.setAutomode(selectedPosition);
                    refresh();
                }
                break;
            case REQUEST_TO_PLFZ:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditTextInfoWindow.RESULT_VALUE);
                    double range = Double.valueOf(editedValue);
                    switch (SysUtils.getInstance().dis_mode) {
                        default:
                            break;
                        case 1:
                        case 4:
                            //ft => m
                            range = range / 3.2808399;
                            break;
                        case 2: {
                            //yd => m
                            range = range / 1.0936133;
                        }
                        break;
                    }
                    _setInfo.setPhfz(range);
                    _setInfo.save(this);
                    refresh();
                }
                break;
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    switch (data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1)) {
                        case 0:
                            _setInfo.reset();
                            _setInfo.save(this);
                            refresh();
                            break;
                        default:
                            break;
                    }
                }
                break;
        }
    }


    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {

    }
    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}
